﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using System.IO;
using DC.BLL;
using DtCms.Common;
using System.Drawing;

namespace DC.BLL
{
    public partial class NewCard : DtCms.Web.UI.ManagePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!this.IsPostBack)
            {

                List<CardSeller> seller = XmlProvider.GetAllCardSeller();
                foreach (CardSeller cs in seller)
                {
                    this.ddlClassId.Items.Add(new ListItem(cs.SellerNamer, cs.SellerId.ToString()));
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            CouponItemCard  card= new CouponItemCard();
            card.CardName = this.CardName.Text;
            card.ExpiredDate = DateTime.Parse(this.ExpiredDate.Text);
            card.Price = float.Parse(this.Price.Text);
            card.Seller = Int16.Parse(this.ddlClassId.SelectedValue);
            card.SellerName = this.ddlClassId.SelectedItem.Text;

            string UploadPath = Server.MapPath(AppSetting.UploadPath);
            string fullPaht = Path.Combine(UploadPath, "DiscountCoupon");
            string guid = Guid.NewGuid().ToString() + Path.GetExtension(this.AttachmentFile.FileName);
            string extention = Path.Combine(fullPaht, guid);
            string thumb = Path.Combine(Path.Combine(fullPaht,"thumb"), guid);

            this.AttachmentFile.SaveAs(extention);
            DC.BLL.Imaging.FasterThumb(extention, thumb, 100,100);
            

            card.CreateTime = DateTime.Now;
            card.CardUrl = System.IO.Path.Combine(AppSetting.ImgBaseUrl,"DiscountCoupon/"+ guid);
            card.ThumbUrl = System.IO.Path.Combine(AppSetting.ImgBaseUrl, "DiscountCoupon/thumb/" + guid);
            XmlProvider.ItemCardList.Add(card);
            XmlProvider.SaveAllItemCard();

            this.Price.Text = "";
            this.CardName.Text = "";
            JscriptPrint("保存成功", "" , "success");
        }
    }
}
